package com.sias.reflection;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author 吴文杰
 * @version 1.0
 * @createTime 2023-05-02 15:43:48
 */
@SuppressWarnings("all")
public class ReflectionUtils {
  public static void main(String[] args) {

  }
  /**
   * 第一组API
   */
  @Test
  public void api_01() throws Exception {
    Class<?> cls = Class.forName("com.sias.reflection.Person");
    //获取全类名
    System.out.println("======================获取全类名==================");
    System.out.println(cls.getName());//com.sias.reflection.Person
    //获取简单类名
    System.out.println("======================获取简单类名==================");
    System.out.println(cls.getSimpleName());//Person

    //获取所有public修饰的属性，包含本类以及父类
    System.out.println("======================获取所有public修饰的属性，包含本类以及父类==================");
    Field[] fields = cls.getFields();
    for (Field field : fields) {
      System.out.println("本类及父类的属性="+field.getName());//本类及父类的属性=nam 本类及父类的属性=hobby
    }
    //获取本类中所有属性
    System.out.println("======================获取本类中所有属性==================");
    Field[] declaredFields = cls.getDeclaredFields();
    for (Field field : declaredFields) {
      System.out.println(field.getName());
    }

    //获取所有public修饰的方法，包含本类以及父类
    System.out.println("======================获取所有public修饰的方法，包含本类以及父类==================");
    Method[] methods = cls.getMethods();
    for (Method method : methods) {
      System.out.println(method.getName());
    }

    //获取本类中的所有方法
    System.out.println("======================获取本类中的所有方法==================");
    Method[] clsDeclaredMethods = cls.getDeclaredMethods();
    for (Method method : clsDeclaredMethods) {
      System.out.println(method.getName());
    }

    //获取本类所有public修饰的构造器
    System.out.println("======================获取本类所有public修饰的构造器==================");
    Constructor<?>[] constructors = cls.getConstructors();
    for (Constructor<?> constructor : constructors) {
      System.out.println(constructor.getName());
    }

    //获取本类的所有构造器
    System.out.println("======================获取本类的所有构造器==================");
    Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors();
    for (Constructor<?> declaredConstructor : declaredConstructors) {
      System.out.println(declaredConstructor.getName());
    }
    //以package形式返回 包信息
    System.out.println("======================以package形式返回 包信息==================");
    System.out.println(cls.getPackage());

    //以Class形式返回父类信息
    System.out.println("======================以Class形式返回父类信息==================");
    System.out.println(cls.getSuperclass());

    //以Class[]形式返回接口信息
    System.out.println("======================以Class[]形式返回接口信息==================");
    Class<?>[] interfaces = cls.getInterfaces();
    for (Class<?> anInterface : interfaces) {
      System.out.println(anInterface.getName());
    }

    //以Annotation[]形式返回注解信息
    System.out.println("======================以Annotation[]形式返回注解信息==================");
    Annotation[] annotations = cls.getAnnotations();
    for (Annotation annotation : annotations) {
      System.out.println(annotation);
    }
  }

  /**
   * 第二组API
   */
  @Test
  public void api_02() throws Exception {
    Class<?> cls = Class.forName("com.sias.reflection.Person");
    Field[] declaredFields = cls.getDeclaredFields();
    for (Field field : declaredFields) {
      /**
       * 默认修饰符 0  public 1 private 2 protected 4  static 8 final 16
       *
       * protected static int age   ====== 12
       */
      System.out.println("本类中所有属性："+field.getName()+"\t该属性的修饰符="+field.getModifiers()+
              "\t该属性的类型="+field.getType());
    }
  }

  /**
   * 第三组API
   */
  @Test
  public void api_03() throws Exception {
    Class<?> cls = Class.forName("com.sias.reflection.Person");
    Method[] methods = cls.getDeclaredMethods();
    for (Method method : methods) {
      System.out.println("本类中所有方法："+method.getName()+"\t该方法的修饰符="+method.getModifiers()+
              "\t该方法的返回类型="+method.getReturnType());
      Class<?>[] parameterTypes = method.getParameterTypes();
      for (Class<?> parameterType : parameterTypes) {
        System.out.println("该方法的参数数组="+parameterType);
      }
    }
  }

  /**
   * 第四组API
   */
  @Test
  public void api_04() throws Exception {
    Class<?> cls = Class.forName("com.sias.reflection.Person");
    Constructor<?>[] constructors = cls.getConstructors();
    for (Constructor<?> constructor : constructors) {
      System.out.println("构造方法全类名："+constructor.getName()+"\t该构造方法的修饰符类型："+constructor.getModifiers());
      Class<?>[] parameterTypes = constructor.getParameterTypes();
      for (Class<?> parameterType : parameterTypes) {
        System.out.println(parameterType);
      }
    }
  }
}
@Deprecated
class Person extends A implements IA,IB{
  public String name;
  protected static int age;
  String job;
  private double salary;


  public void m1(int a,String name){}
  protected void m2(){}
  void m3(){}
  private void m4(){}

  public Person() {
  }

  public Person(String name, int age, String job, double salary) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.salary = salary;
  }

  private Person(String name) {
    this.name = name;
  }
}

class A{
  public String hobby;

  public A() {
  }
}

interface IA{

}
interface IB{

}
